<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn" data-type="add">添加</button>
        </div>

        <div class="layui-card-body">
            <table id="{$lay_table}" lay-filter="{$lay_table}"></table>
            <script type="text/html" id="setTpl">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['index', 'form', 'set', 'table'], function () {
        const $ = layui.$;
        const admin = layui.admin;
        const form = layui.form;
        const table = layui.table;
        form.render();

        table.render({
            elem: '#{$lay_table}',
            url: "{:url('sms_template/listTemplate')}",
            cols: [
                [
                    {field: 'id', width: 80,title: 'ID'},
                    {field: 'code', width: 160, title: '编号', sort: true},
                    {field: 'comment', title: '备注'},
                    {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#setTpl'}
                ]
            ],
            page: true,
            limit: 50,
            cellMinWidth: 150,
            text: '对不起，加载出现异常！'
        });

        //事件
        const active = {
            add: function () {
                const index = layer.open({
                    type: 2,
                    title: '添加模板',
                    content: "{:url('sms_template/templateForm')}",
                    maxmin: true,
                    area: ['550px', '550px']
                });
                layer.full(index);
            },
        };

        //监听工具条
        table.on('tool({$lay_table})', function (obj) {
            const data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除此模板吗？', function (index) {
                    admin.req({
                        url: "{:url('sms_template/delTemplate')}",
                        data: {
                            id: data.id
                        },
                        done: function (res) {
                            obj.del();
                            layer.msg('已删除');
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                const index = layer.open({
                    type: 2,
                    title: '编辑模板',
                    content: "{:url('sms_template/templateForm')}?id=" + data.id,
                    maxmin: true,
                    area: ['550px', '550px']
                });
                layer.full(index);
            }
        });

        $('.layui-btn.layuiadmin-btn').on('click', function () {
            const type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
